package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.mysql.UserVideoDao;
import com.juyu.miliao.api.dao.mysql.VideoCommentDao;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.VideoComment;
import com.juyu.miliao.api.domain.vo.VideoCommentVO;
import com.juyu.miliao.api.yidun.servcie.AntispamService;
import com.juyu.miliao.common.constant.PageConstant;
import com.juyu.miliao.common.domain.BaseQuery;
import com.juyu.miliao.common.domain.PageIndex;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.netease.NetEaseCloudUserService;
import com.juyu.miliao.common.util.CommonUtil;
import com.juyu.miliao.common.util.PageInfoUtil;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jh
 * 10:57
 */
@Service
public class VideoCommentService {

    @Resource
    private VideoCommentDao videoCommentDao;

    @Resource
    private UserInfoDao userInfoDao;

    @Resource
    private AntispamService antispamService;

    @Resource
    private UserVideoDao userVideoDao;


    /**
     * 根据videoId查询列表
     *
     * @param videoId   videoId
     * @param baseQuery baseQuery
     * @return List<VideoComment>
     */
    public VideoCommentVO list(Long videoId, BaseQuery baseQuery) {
        PageInfoUtil.cleanOrder(baseQuery);
        Integer rows = videoCommentDao.countVideoComment(videoId);
        PageIndex pageIndex = PageInfoUtil.getPageIndex(baseQuery.getPageNum(), baseQuery.getPageSize(), rows, false);
        List<VideoComment> list;
        if (pageIndex.getOffset() == PageConstant.OUT_OF_ROWS) {
            list = new ArrayList<>();
        } else {
            list = videoCommentDao.listVideoComment(videoId, pageIndex.getOffset(), pageIndex.getPageSize());
        }
        VideoCommentVO vo = new VideoCommentVO();
        vo.setRows(rows);
        vo.setComments(list);
        return vo;
    }

    /**
     * 保存对话
     *
     * @param userId       userId
     * @param videoComment videoComment
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void save(Long userId, VideoComment videoComment) {
        //处理content
        String content = CommonUtil.null2String(videoComment.getContent());
//        Result result = antispamService.onlineCheck(videoComment.getContent());
//        if (result.getCode() == ResultConstant.ERROR_CODE) {
//            throw new DataValidateException(result.getMsg());
//        }
        try {
            String temp = NetEaseCloudUserService.getYunTextCheck(content);
            JSONObject json = JSONObject.parseObject(temp);
            int code = json.getIntValue("code");
            if(code == 200){
                JSONObject result = json.getJSONObject("result");
                int action = result.getIntValue("action");
                if(action==0){
                    videoComment.setContent(content);
                    //用户信息
                    videoComment.setUserId(userId);
                    User user = userInfoDao.getUserInfoById(userId);
                    videoComment.setNickname(user.getNickName());
                    videoComment.setIcon(user.getIcon());
                    videoCommentDao.saveVideoComment(videoComment, videoComment.getVideoId());
                    userVideoDao.addCommentNum(videoComment.getVideoId());
                }else{
                    throw new ConsumeRuntimeException("4112","评论内容违规", HttpStatus.BAD_REQUEST);
                }
            }else{
                throw new ConsumeRuntimeException("检测结果异常:"+temp);
            }
        }catch (IOException e){
            throw new ConsumeRuntimeException("error检测错误:"+e.getMessage());
        }


    }


}
